<?php
/**
 * Created By Project
 * Author: 陈东
 * Copyright：陈东
 * Source Code：陈东
 * Explain：I shall not authorize for any other commercial purposes, law breakers must be prosecuted.
 */
namespace Admin\Controller;
use Think\Controller;

class ManagesController extends Controller {

    protected function newobj(){
        return M('adminmanages');
    }

    public function index(){

        $res = $this->getallinfo();
        $this->assign('data',$res);
		$this->display();
    }

    /**
     * 获取所有后台分类结构
     */
    public function getallinfo(){
        $manages = $this->newobj();
        $where['mid'] = 0;
        $where['status'] = 1;
        $res = $manages->where($where)->order('mid asc,msort asc')->select();
        if($res){
            foreach($res as &$v){
                $v['submenu'] = $this->gettypelistmid($v['id']);
            }
        }
        return $res;
    }

    //根据mid获取分类列表
    protected function gettypelistmid($mid = null){

        $manages = $this->newobj();
        $res = $manages->where(array('mid'=>$mid,'status'=>1))->order('mid asc,msort asc')->select();
        return $res;
    }

    //添加
    public function madd(){

        $rules = array(
            array('manage_name','require','分类名称不能为空！'),
            //array('manage_name','','已经存在此分类名称！',0,'unique',1)
        );
        $manages = $this->newobj();
        if(!$manages->validate($rules)->create()){
            $this->error($manages->getError(),'',1);
        }else{
            $result = $manages->add();
            if($result>=0){
                $this->success('添加成功', U('index'));
            }else{
                $this->error('添加失败', '', 1);
            }
        }
    }

    //编辑
    public function medit(){

        $rules = array(
            array('manage_name','require','分类名称不能为空！')
        );
        $manages = $this->newobj();
        $mid = I('post.editid');
        if(!$manages->validate($rules)->create()){
            $this->error($manages->getError(),'',1);
        }else{
            $result = $manages->where(array('id'=>$mid))->save();
            if($result>=0){
                $this->success('编辑成功', U('index'));
            }else{
                $this->error('编辑失败', '', 1);
            }
        }
    }

    //编辑页面
    public function edit($id){

        $res = $this->getallinfo();
        $this->assign('mlist',$res);
        $this->assign('edit',1);
        $this->assign('m',$this->gettypeinfoid($id));
        $this->display('menuadd');
    }

    //根据ID获取分类信息
    protected function gettypeinfoid($id = null){

        $manages = $this->newobj();
        $res = $manages->find($id);
        return $res;
    }

    public function menuadd(){

        $res = $this->getallinfo();
        $this->assign('mlist',$res);
        $this->display();
    }

    //逻辑删除
    public function delete($id){
        $manages = $this->newobj();
        $manages->status = 0;
        $res = $manages->where(array('id'=>$id))->save();
        if($res>=0){
            $this->success('删除成功',U('index'));
        }else{
            $this->error('删除失败','',1);
        }
    }

    /* ============================版块业务============================ */
    protected function plate_obj(){
        return M('platemanages');
    }

    /**
     * 执行转交业务逻辑
     */
    public function jur(){
        $ma_id = I('post.ma_id');
        $user_id = I('post.user_id');
        if(!empty($ma_id) && !empty($user_id)){
            $type = D(C('TABLE_PLATEMANAGES'));
            $data['userid'] = $user_id;
            $res = $type->where("id in ($ma_id)")->save($data);
        }
        $msg = $res ? array('error' => 0) : array('error' => 1);
        echo json_encode($msg);
    }

    /**
     * 版块管理页面
     */
    public function plate(){
        //获取用户列表
        $u = A("User");
        $result = $u->userinfo();
        $this->assign('userlist',$result);
        $this->assign('getURL',getURL());
        $res = $this->get_all_plate_info();
        $this->assign('data', $res);
        $this->assign('accounttype', session('userinfo')['account_type']);
        $this->display();
    }

    public function plate_browse($id = null){
        $u = A("User");
        $result = $u->userinfo();
        $this->assign('userlist',$result);
        $res = $this->get_all_plate_info_s($id);
        $plateinfo = $this->get_plate_id_info($id);
        $this->assign('plateinfo', $plateinfo);
        $this->assign('data', $res);
        $this->display();
    }

    /**
     * 根据版块ID查询当前版块的信息
     */
    protected function get_plate_id_info($id = null){
        $plate = $this->plate_obj();
        $res = $plate->find($id);
        return $res;
    }

    protected function get_all_plate_info_s($id){
        $plate = $this->plate_obj();
        $where['mid'] = $id;
        $where['status'] = 1;
        $res = $plate->where($where)->order('mid asc, plate_sort asc')->select();
        return $res;
    }

    /**
     * 根据typeid获取所属版块信息
     */
    protected function get_type_plate_info($typeid, $id = null){
        $plate = $this->plate_obj();
        if(!empty($id)){
            $res = $plate->where(array('plate_type' => $typeid, 'id' => $id,'status' => 1))->order('mid asc, plate_sort asc')->select();
        }else{
            $res = $plate->where(array('plate_type' => $typeid, 'mid' => 0,'status' => 1))->order('mid asc, plate_sort asc')->select();
        }
        if($res){
            foreach($res as &$v){
                $v['subplate'] = $this->get_subplate_info($v['id']);
            }
        }
        return $res;
    }

    /**
     * 获取所有版块信息
     */
    public function get_all_plate_info(){
        $plate = $this->plate_obj();
        $res = $plate->where(array('mid' => 0,'status' => 1))->order('plate_sort desc, mid desc')->select();
        if($res){
            foreach($res as &$v){
                $v['subplate'] = $this->get_subplate_info($v['id']);
            }
        }
        return $res;
    }

    /**
     * 根据mid获取子版块信息
     */
    protected function get_subplate_info($mid = null){
        $plate = $this->plate_obj();
        $res = $plate->where(array('mid' => $mid))->order('mid asc,plate_sort asc')->select();
        if($res){
            foreach($res as &$v){
                $v['subplates'] = $this->get_subplate_info($v['id']);
            }
        }
        return $res;
    }

    /**
     * 添加版块页面
     */
    public function plate_add($typeid, $id = null){
        $res = $this->get_type_plate_info($typeid, $id);
        $this->assign('plate_list',$res);
        $this->display();
    }

    /**
     * 添加版块业务
     */
    public function plateadd(){
        $sesion = session('userinfo');
        $rules = array(
            array('plate_name', 'require', '版块名称不能为空！'),
            array('plate_name', '', '已经存在此版块名称！', 0, 'unique', 1)
        );
        $plate = $this->plate_obj();
        if(!$plate->validate($rules)->create()){
            $this->error($plate->getError(),'',1);
        }else{
            $content 		    = html_entity_decode(I('post.content'));
            if(empty(I('post.mid'))) $plate->mid = I('post.emid');
            $plate->plate_type  = I('post.typeid');
            $plate->userid      = $sesion['id'];
            $plate->content     = cutArticle($content, 300);
            $plate->videopath  = html_entity_decode(I('post.videopath'));
            $result = $plate->add();
            if($result>=0){
                $this->success('添加成功', U('plate'));
            }else{
                $this->error('添加失败', '', 1);
            }
        }
    }

    /**
     * 删除
     */
    public function plate_delete($id){
        $del_check = $this->del_check_subplate($id);
        if($del_check){
            $this->error('请先删除子版块', '', 2);
        }else{
            $plate = $this->plate_obj();
            $res = $plate->delete($id);
            if($res){
                $this->success('删除成功');
            }else{
                $this->error('删除失败', '', 1);
            }
        }
    }

    /**
     * 删除前先判断是否有子版块
     */
    protected function del_check_subplate($id = null){
        $result = false;
        $plate = $this->plate_obj();
        $num = $plate->where(array('mid' => $id))->count();
        $result = $num > 0 ? true : false;
        return $result;
    }

    /**
     * 编辑版块页面
     */
    public function plate_edit($id){
        $plate = $this->plate_obj();
        $res = $plate->where(array('id' => $id))->find();
        $plate_info = $this->get_all_plate_info();
        $this->assign('edit', 1);
        $this->assign('pedit', I('get.pedit'));
        $this->assign('plate_list', $plate_info);
        $this->assign('p_resutl', $res);
        $this->display('plate_add');
    }

    /**
     * 版块编辑
     */
    public function plateedit(){
        $id = I('post.editid');
        $rules = array(
            array('plate_name', 'require', '版块名称不能为空！'),
        );
        $plate = $this->plate_obj();
        if(!$plate->validate($rules)->create()){
            $this->error($plate->getError(),'',1);
        }else{
            $content 		   = html_entity_decode(I('post.content'));
            $plate->content    = cutArticle($content, 300);
            $plate->videopath  = html_entity_decode(I('post.videopath'));
            $result = $plate->where(array('id' => $id))->save();
            if($result >= 0){
                $this->success('编辑成功', U('plate'));
            }else{
                $this->error('编辑失败', '', 1);
            }
        }
    }

    /**
     * 资源文章搜索
     */
    public function search(){
        $key = trim(I('post.key'));
        if(!empty($key)){
            $p_content = D(C('TABLE_PLATECONTENT'));
            $where['title'] = array('LIKE',"%".$key."%");
            $where['plate_id'] = array('not in','52,53,56,57');
            $result = $p_content->where($where)->select();
            $this->assign('plate_list',$result);
            $this->display('Pcontent/content_list');
        }
    }



}